package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Address;
import com.sansi.pencilbook.entity.User;
import com.sansi.pencilbook.entity.UserAndAddress;
import com.sansi.pencilbook.service.AddressServer;
import com.sansi.pencilbook.service.UserAndAddressServer;
import com.sansi.pencilbook.service.UserServer;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 功能说明: 地址控制器
 *
 * @author lsy13
 */
@Slf4j
@RestController
@RequestMapping("/api/address")
public class AddressController extends VerifyController implements BaseController{

    @Autowired
    private AddressServer addressServer;

    @Autowired
    private UserServer userServer;

    @Autowired
    private UserAndAddressServer userAndAddressServer;

    @GetMapping("/u/getAddressByUser")
    public ResponseDO getAddressByUser(HttpServletRequest request) {
        log.info("获得用户的收货地址");
        Cookie[] cookies = request.getCookies();
        String userAccount = null;
        for (Cookie cookie : cookies) {
            if ("userAccount".equals(cookie.getName())) {
                userAccount = cookie.getValue();
            }
        }
        if (checkUser(userAccount)) {
            User user = (User) userServer.getUserByAccount(userAccount).getData();
            Result result = addressServer.getAddressesByUser(user.getUserId());
            return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                    ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @PostMapping("/u/deleteAddress")
    public ResponseDO deleteAddress(@RequestParam("address") String address, HttpServletRequest request) {
        log.info("删除收货地址");
        Cookie[] cookies = request.getCookies();
        String userAccount = null;
        for (Cookie cookie : cookies) {
            if ("userAccount".equals(cookie.getName())) {
                userAccount = cookie.getValue();
            }
        }
        if (checkUser(userAccount)) {
            User user = (User) userServer.getUserByAccount(userAccount).getData();
            Address address1 = (Address) addressServer.getAddressByIdAndAddress(user.getUserId(), address).getData();
            Result result = addressServer.deleteAddress(address1);
            return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                    ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @PostMapping("/a/deleteAddress")
    public ResponseDO deleteAddress(@RequestBody Address address, HttpServletRequest request) {
        if (checkAdmin(request)) {
            Result result = addressServer.deleteAddress(address);
            return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @PostMapping("/a/deleteAddresses")
    public ResponseDO deleteAddresses(@RequestBody List<Address> addresses, HttpServletRequest request) {
        if (checkAdmin(request)) {
            Result result = addressServer.deleteAddresses(addresses);
            return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @PostMapping("/u/addUserAddress")
    public ResponseDO addAddress(@RequestBody Address address, HttpServletRequest request) {
        log.info("添加用户的收货地址");
        Cookie[] cookies = request.getCookies();
        String userAccount = null;
        for (Cookie cookie : cookies) {
            if ("userAccount".equals(cookie.getName())) {
                userAccount = cookie.getValue();
            }
        }
        if (checkUser(userAccount)) {
            User user = (User) userServer.getUserByAccount(userAccount).getData();
            address.setUserId(user.getUserId());
            address.setStoreId(0);
            Result result = addressServer.addAddress(address);
            return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                    ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @GetMapping("/a/getAllAddress")
    public ResponseDO getAllAddress(HttpServletRequest request) {
        if (checkAdmin(request)) {
            Result result = userAndAddressServer.getAllAddress();
            return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                    ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @PostMapping("/u/addStoreAddress")
    public ResponseDO addStoreAddress(@RequestBody Address address, HttpServletRequest request) {
        log.info("添加书铺的收货地址");
        int storeId;
        if ((storeId=getStore(request))!=0 || checkAdmin(request)) {
            Cookie[] cookies = request.getCookies();
            String userAccount = null;
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            if (userAccount == null) {
                return ResponseDOUtil.setResponseDO(false, "用户账户不存在");
            }
            User user = (User) userServer.getUserByAccount(userAccount).getData();
            address.setUserId(user.getUserId());
            address.setStoreId(storeId);
            Result result = addressServer.addAddress(address);
            return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @GetMapping("/u/getStoreAddress")
    public ResponseDO getStoreAddress(HttpServletRequest request) {
        log.info("获取书铺的收货地址");
        int storeId;
        if ((storeId=getStore(request))!=0 || checkAdmin(request)) {
            Result result = addressServer.getStoreAddress(storeId);
            return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @PostMapping("/u/updateAddress")
    public ResponseDO updateAddress(HttpServletRequest request, @RequestBody Address address) {
        log.info("更新书铺的收货地址");
        if (checkStore(request)|| checkAdmin(request)) {
            Result result = addressServer.modifyAddress(address);
            return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }
}
